<?php
/**
 * Project: story-server
 * File Created at 2017-02-23
 * Copyright 2014 keyed.cn All right reserved. This software is the
 * confidential and proprietary information of keyed.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with keyed.cn .
 */

namespace lib\util;

class Request
{
    public static function request($url, $data = null, $isPost = false, $isShowHeader = 0, $timeout = 30)
    {
        $process = curl_init($url);
        curl_setopt($process, CURLOPT_HEADER, $isShowHeader ? 1 : $isShowHeader);                    // 显示返回的Header区域内容

        curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($process, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($process, CURLOPT_TIMEOUT, $timeout); // 设置超时限制防止死循环
        curl_setopt($process, CURLOPT_AUTOREFERER, 1); // 自动设置Referer

        curl_setopt($process, CURLOPT_CONNECTTIMEOUT, $timeout);
        if ($isPost) {
            curl_setopt($process, CURLOPT_POST, 1);               // 发送一个常规的Post请求
            curl_setopt($process, CURLOPT_POSTFIELDS, $data);  // Post提交的数据包
        } else {
            curl_setopt($process, CURLOPT_POST, 0);
        }

        curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);

        $return     = curl_exec($process);
        $statusCode = curl_getinfo($process, CURLINFO_HTTP_CODE);

        if ($statusCode !== 200) {
            $return     = curl_exec($process);
            $statusCode = curl_getinfo($process, CURLINFO_HTTP_CODE);
        }
        curl_close($process);

        return $return;
    }

    public static function get($url, $second = 2000)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, $second);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
        curl_setopt($curl, CURLOPT_URL, $url);

        $res = curl_exec($curl);
        curl_close($curl);

        return $res;
    }

    //HTTP Weixin JSON工具
    public static function postJsonData($url, $data_string)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data_string)]);
        ob_start();
        curl_exec($ch);
        $return_content = ob_get_contents();
        ob_end_clean();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        return [$return_code, $return_content];
    }

    //HTTP post 工具
    public static function httpWeixinPost($url, $post_data)
    {
        // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        if (!empty($post_data)) {
            curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
            curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // Post提交的数据包
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        // 发出请求
        $response = curl_exec($curl);
        curl_close($curl); // 关闭CURL会话
        return $response;
    }

    /**
     * passport用户中心请求参数.
     *
     * @param string $apiVersion 版本号
     * @param string $params     post 数据
     * @param string $class      类名
     * @param string $method     方法名
     *
     * @return array|mixed
     */
    public static function getPassportRequestInfo($apiVersion, $params, $class, $method)
    {
        $config          = loadconf('passport/passport');
        $qbaotingApiInfo = $config[2];

        $getParams['appid'] = $qbaotingApiInfo['appid'];
        $getParams['time']  = time();
        $host               = $qbaotingApiInfo['passport_host'] . '/' . $apiVersion . '/' . $class . '/' . $method;
        $getParams['hash']  = md5($qbaotingApiInfo['key'] . $apiVersion . $class . '/' . $method . $qbaotingApiInfo['appid'] . $getParams['time']);
        $host               = $host . '?' . http_build_query($getParams);

        return ['params' => $params, 'host' => $host];
    }

    /**
     * 获取用户真实ip地址
     *
     * @return string
     */
    public static function getClientIP()
    {
        $ip = '';
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('REMOTE_ADDR')) {
            $ip = getenv('REMOTE_ADDR');
        }

        return $ip;
    }
}
